﻿using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Windows;
using System;
using System.Windows.Navigation;
using Microsoft.Xna.Framework.Input.Touch;
using BlawkayMobileLibrary;

namespace Bounce
{
    public partial class GamePage : PhoneApplicationPage
    {

        public GamePage()
        {
            InitializeComponent();

            // Get the content manager from the application
            ContentManager = (Application.Current as App).Content;

            // Create a timer for this page
            Timer = new GameTimer();
            Timer.UpdateInterval = TimeSpan.Zero;
            //TimeSpan.FromTicks(166667);

            Timer.Update += OnUpdate;
            Timer.Draw += OnDraw;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            NextScreen = CurrentScreen;
            LoadScreen();

            // Start the timer
            Timer.Start();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            Timer.Stop();
            
            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (CurrentScreen != App.ScreenType.Menu)
            {
                SetNextScreen(App.ScreenType.Menu);
                e.Cancel = true;
            }
        }

        App.ScreenType? NextScreen = null;
        public void SetNextScreen(App.ScreenType oType)
        {
            NextScreen = oType;
        }

        public void LoadScreen()
        {
            if (NextScreen == null)
                return;

            CurrentScreen = (App.ScreenType)NextScreen;

            switch (CurrentScreen)
            {
                case App.ScreenType.ManyBalls:
                    Screen = new ManyBalls(this); break;
                case App.ScreenType.AimIt:
                    Screen = new AimIt(this); break;
                case App.ScreenType.PuttPutt:
                    Screen = new PuttPutt(this); break;
                case App.ScreenType.TiltingBalls:
                    Screen = new TiltingBalls(this); break;
                case App.ScreenType.Menu:
                    Screen = new Menu(this); break;
            }

            Screen.Initialize(
                (Application.Current as App).Content,
                SharedGraphicsDeviceManager.Current.GraphicsDevice);

            NextScreen = null;
        }

        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            if (NextScreen != null)
                LoadScreen();

            Screen.Update((float)e.ElapsedTime.TotalSeconds);
        }

        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.GhostWhite);
            Screen.Draw((float)e.ElapsedTime.TotalSeconds);
        }



        App.ScreenType CurrentScreen
        {
            get { return (Application.Current as App).CurrentScreen; }
            set 
            { 
                (Application.Current as App).CurrentScreen 
                    = (Application.Current as App).PreviousScreen;
                
                (Application.Current as App).CurrentScreen = value;
            }
        }


        ContentManager ContentManager
        {
            get;
            set;
        }

        GameTimer Timer
        {
            get;
            set;
        }

        Screen Screen
        {
            get;
            set;
        }

    }
}